package cat.joronya.supermanager.service;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.content.Context;
import android.net.ConnectivityManager;
import android.util.Log;
import cat.joronya.supermanager.Supermanager;
import cat.joronya.supermanager.privateleague.PrivateLeague;

public class PrivateLeaguesSearchSync extends Object
{
	private static final String TAG = "supermanager";
	
	// service pel contexte
	private SupermanagerService service;
	
	// el client http on fer les peticions
	private DefaultHttpClient client = null;
	
	// status de retorn
	public int status = -1;
	
	private final String SEARCH_URL = "http://supermanager.acb.com/inscribir.php";
	
	public ArrayList<PrivateLeague> privateLeagues; 
	
	public PrivateLeaguesSearchSync(SupermanagerService service, DefaultHttpClient client)
	{
		this.service = service;
		this.client = client;
		privateLeagues = new ArrayList<PrivateLeague>();
	}
	
	protected void searchWithText(String search) 
	{
		try
		{
			// checkejem per connectivitat
			ConnectivityManager cm = (ConnectivityManager) service.getSystemService(Context.CONNECTIVITY_SERVICE);
			boolean online = cm.getActiveNetworkInfo().isConnectedOrConnecting();
			
			if( !online )
				throw new ConnectivityException();
			
			// POST: inscribir search
			HttpPost httpPost = new HttpPost(SEARCH_URL);
			
	        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
	        nvps.add(new BasicNameValuePair("buscanombre", search));
	        nvps.add(new BasicNameValuePair("busqueda", "1"));
	        nvps.add(new BasicNameValuePair("x", "0"));
	        nvps.add(new BasicNameValuePair("y", "0"));
	        
	        httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.ISO_8859_1));
	        
	        HttpResponse response = client.execute(httpPost);
			ByteArrayOutputStream os = new ByteArrayOutputStream();
	        response.getEntity().writeTo(os);
	        String searchPage = os.toString(HTTP.ISO_8859_1);
	        
	        PrivateLeagueSearchParser privateLeagueSearchParser = new PrivateLeagueSearchParser();
	        privateLeagues = privateLeagueSearchParser.parse(searchPage);
			
			// parsejat ok
	        status = Supermanager.PRIVATE_LEAGUE_SEARCH_OK;
	        Log.d(TAG, "PrivateLeagueSearchSync status: PRIVATE_LEAGUE_SEARCH_OK - "+status);
	        return;
		}
		catch(ConnectivityException ce){
			// error de connectivitat
			status = Supermanager.CONNECTIVITY_ERROR_STATUS;
	        Log.d(TAG, "PrivateLeagueSearchSync status: CONNECTIVITY_ERROR_STATUS - "+status);
			return;
		}
		catch(Exception e){
			// altre error, notifiquem i anem offline
			status = Supermanager.OTHER_ERROR_STATUS;
	        Log.d(TAG, "PrivateLeagueSearchSync status: OTHER_ERROR_STATUS - "+status);
			return;
		}
	}

	protected void searchWithGroup(String group) 
	{
		try
		{
			// checkejem per connectivitat
			ConnectivityManager cm = (ConnectivityManager) service.getSystemService(Context.CONNECTIVITY_SERVICE);
			boolean online = cm.getActiveNetworkInfo().isConnectedOrConnecting();
			
			if( !online )
				throw new ConnectivityException();
			
			// GET: inscribir search
			HttpGet httpGet = new HttpGet(SEARCH_URL+"?grupo="+group);
			
	        HttpResponse response = client.execute(httpGet);
			ByteArrayOutputStream os = new ByteArrayOutputStream();
	        response.getEntity().writeTo(os);
	        String searchPage = os.toString(HTTP.ISO_8859_1);
	        
	        PrivateLeagueSearchParser privateLeagueSearchParser = new PrivateLeagueSearchParser();
	        privateLeagues = privateLeagueSearchParser.parse(searchPage);
			
			// parsejat ok
	        status = Supermanager.PRIVATE_LEAGUE_SEARCH_OK;
	        Log.d(TAG, "PrivateLeagueSearchSync status: PRIVATE_LEAGUE_SEARCH_OK - "+status);
	        return;
		}
		catch(ConnectivityException ce){
			// error de connectivitat
			status = Supermanager.CONNECTIVITY_ERROR_STATUS;
	        Log.d(TAG, "PrivateLeagueSearchSync status: CONNECTIVITY_ERROR_STATUS - "+status);
			return;
		}
		catch(Exception e){
			// altre error, notifiquem i anem offline
			status = Supermanager.OTHER_ERROR_STATUS;
	        Log.d(TAG, "PrivateLeagueSearchSync status: OTHER_ERROR_STATUS - "+status);
			return;
		}
	}

}
